package com.antipov.odp.client;

import com.antipov.odp.IOHelper;
import com.antipov.odp.pojo.I;

import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.io.StreamTokenizer;

/**
 * Created with IntelliJ IDEA.
 * User: Andrew Antipov
 * Date: 18.02.14
 * Time: 22:48
 */
public class IReader {

	private Reader reader;
	private StreamTokenizer tok;
	private int n;

	public IReader(String fileName) {
		try {
			reader = new FileReader(fileName);
			init();
		} catch (IOException e) {
			throw new RuntimeException("Cannot read data", e);
		}
	}

	private void init() throws IOException {
		tok = new StreamTokenizer(reader);
		n = IOHelper.nextInt(tok);
	}

	public boolean hasNext() {
		return n > 0;
	}

	public I next() {
		if(n == 0) {
			throw new RuntimeException();
		}
		try {
			I res = IOHelper.readNext(tok);
			n--;
			return res;
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}



}
